﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxClasses;

namespace TireService.Web.View.ServiceView
{
    public partial class TaskList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Bind containers to dropdown list
            BindContainerDropdownList(db);

            // Bind tasktype to dropdown list
            BindTaskTypeDropdownList(db);
        }

        private void BindTaskList(TireServiceDB db, int ContainerId, int TaskTypeId, int TaskTypeStatusId) 
        {
            gvTask.DataSource = db.GetTasksExtendedView().Where(p => p.ContainerId == ContainerId & p.TaskTypeId == TaskTypeId & p.TaskTypeStatusId == TaskTypeStatusId).ToList();
            gvTask.DataBind();
        }

        private void BindContainerDropdownList(TireServiceDB db)
        {
            ddlContainer.DataSource = db.GetContainers();
            ddlContainer.TextField = "ContainerNumber";
            ddlContainer.ValueField = "ContainerId";
            ddlContainer.DataBind();
        }

        private void BindTaskTypeDropdownList(TireServiceDB db)
        {
            ddlTaskType.DataSource = db.GetTaskTypes();
            ddlTaskType.TextField = "Description";
            ddlTaskType.ValueField = "TaskTypeId";
            ddlTaskType.DataBind();
        }

        private void BindTaskTypeStatusDropdownList(TireServiceDB db, int TaskTypeId)
        {
            ddlTaskTypeStatus.DataSource = db.GetTaskTypeStatus(TaskTypeId);
            ddlTaskTypeStatus.TextField = "Description";
            ddlTaskTypeStatus.ValueField = "TaskTypeStatusId";
            ddlTaskTypeStatus.DataBind();
        }

        protected void ddlTaskTypeStatus_Callback(object source, CallbackEventArgsBase e)
        {
            // Instantiate data layer
            TireServiceDB db = new TireServiceDB();

            BindTaskTypeStatusDropdownList(db, Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Postback function that updates gridview with tasks
        /// </summary>
        protected void gvTask_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            // Get values
            // 0: containerid
            // 1: tasktypeid
            // 2: taskstatustypeid
            string[] values = e.Parameters.Split('|');

            // Instantiate data layer
            TireServiceDB db = new TireServiceDB();

            // Refresh gridview
            BindTaskList(db, Convert.ToInt32(values[0]), Convert.ToInt32(values[1]), Convert.ToInt32(values[2]));
        }

        /// <summary>
        /// Gridview detail function that updates / show details for tasks
        /// </summary>
        protected void gvTaskDetail_DataSelect(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();
            ASPxGridView gvTaskDetail = (sender as ASPxGridView);
            gvTaskDetail.DataSource = db.GetTaskDetailsExtendedView(Convert.ToInt32((sender as ASPxGridView).GetMasterRowKeyValue()));
        }

        /// <summary>
        /// Function that calculates number of taskdetail for a specific task and return string with number
        /// </summary>
        /// <param name="TaskId">Id on task</param>
        /// <returns>String with text "Opgaver: " and the number of taskdetails</returns>
        public string GetTaskDetailCount(int TaskId)
        {
            TireServiceDB db = new TireServiceDB();
            return "Opgaver: " + db.CountTaskDetails(Convert.ToInt32(TaskId)).ToString();
        }

        /// <summary>
        /// Button function for export of gridview
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ddlExport.Value == "0")
            {
                gvExport.WritePdfToResponse();
            }

            if (ddlExport.Value == "1")
            {
                gvExport.WriteXlsToResponse();
            }

            if (ddlExport.Value == "2")
            {
                gvExport.WriteCsvToResponse();
            }

            if (ddlExport.Value == "3")
            {
                gvExport.WriteRtfToResponse();
            }
        }
    }
}